package objectstream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : ObjectOutputStreamDemo
 * @Author : 54061
 * @Date: 2022/2/14 17:51
 */
/*
    NotSerializableException: 没有序列化异常

    如果想把对象写到文件中,对象所在的类必须实现Serializable接口
 */
public class ObjectOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream("Day18\\aaa\\obj.txt"));


        //往文件中写对象
//        oos.writeObject(100);
//        oos.writeObject("你好");
//        oos.writeObject(new Date());
//        oos.writeObject(new Student("001","张胜",18,"武汉"));

        //规矩是: 写对象之前把所有对象装到集合中  写集合对象到文件中  写一次!!
        ArrayList<Object> list = new ArrayList<>();
        list.add(100);
        list.add("你好");
        list.add(new Date());
        list.add(new Student("001","张三",18,"武汉"));

        oos.writeObject(list);

        oos.close();
    }
}
